<template>
  <div class="warp-box">
    <el-row class="search-box" :gutter="10">
      <el-col :span="20">
        <el-input
          style="width:220px"
          v-model="parames.customerName"
          size="small"
          clearable
          placeholder="搜索公司名称"
        ></el-input>
        <span style="margin-left:10px">是否隐藏黑名单：</span>
        <el-select style="width:100px" size="small" @change="search"  v-model="parames.white" placeholder="是否隐藏黑名单">
          <el-option label="是" :value="true"></el-option>
          <el-option label="否" :value="false"></el-option>
        </el-select>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="text" icon="el-icon-s-open" @click="handleSearch">高级搜索</el-button>
      </el-col>
    </el-row>
    <el-row class="main-box channel1234" style="margin:10px -5px;">
      <el-col :span="24">
        <el-button size="small" style="float:left" type="primary" icon="el-icon-plus" @click="handleAdd" v-if="newBtn">新建客户</el-button>
        <el-button size="small" style="float:left" icon="el-icon-download" @click="handleExport" v-if="daochuBtn">导出</el-button>
        <el-button size="small" v-if="zhuanchuBtn" style="float:left" icon="el-icon-connection" @click="handleTrans">分配资源</el-button>
        <el-upload
          v-if="daoruBtn"
          style="float:left;margin-left:10px"
          class="upload-demo"
          :headers="myHeaders"
          :action="fileAction"
          :show-file-list="false"
          accept=".XLS,.xlsx"
          :on-success="handleSucess">
          <el-button size="small" icon="el-icon-upload2">导入</el-button>
        </el-upload>
        <el-button v-if="mobanBtn" size="small" style="float:left;margin-left:10px" icon="el-icon-download" @click="handleExport2">下载模板</el-button>
      </el-col>
      <el-col :span="24">
        <el-table
          :data="tableData"
          border
          v-loading="loading"
          @row-click="handleRow"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
          style="margin-top:14px;"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column
            show-overflow-tooltip
            min-width="140"
            fixed="left"
            prop="customerName"
            label="公司名称"
          ></el-table-column>
          <el-table-column show-overflow-tooltip min-width="120" prop="previousName" label="曾用名"></el-table-column>
          <el-table-column min-width="200" prop="director" label="客户负责人">
            <template slot-scope="scope">
              <div>{{scope.row.director}}(工号：{{scope.row.directorWorkNumber}})</div>
            </template>
          </el-table-column>
          <el-table-column min-width="200" prop="dengji" label="客户等级">
            <template slot-scope='scope'>
              <p v-if="scope.row.customerLevel">{{scope.row.customerLevel.desc}}</p>
            </template>
          </el-table-column>
          <el-table-column min-width="200" prop="laiyuan" label="客户来源类型">
            <template slot-scope='scope'>
              <p v-if="scope.row.customerSourceType">{{scope.row.customerSourceType.desc}}</p>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip min-width="120" prop="customerSource" label="客户来源"></el-table-column>
          <el-table-column  
              width="120"
              label="是否黑名单">
              <template slot-scope="scope">
                <div v-if="!scope.row.blacklisted">否</div>
                <div v-if="scope.row.blacklisted">是</div>
              </template>
          </el-table-column>
          <el-table-column  
              width="120"
              label="是否废业">
              <template slot-scope="scope">
                <div v-if="!scope.row.wasteIndustry">否</div>
                <div v-if="scope.row.wasteIndustry">是</div>
              </template>
          </el-table-column>
          <el-table-column width="80" align="center" label="" fixed="right">
            <template slot-scope="scope">
              <div style="text-align: center;">
                <el-button size="mini" @click="handleRow(scope.row)"  type="text" icon="el-icon-search"></el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="24">
        <pages :parames="parames" @render="checkPage"></pages>
      </el-col>
    </el-row>
    <createdDialog :dialogData="dialogData" @checkRender="getList"></createdDialog>
    <searchDialog :searchData="searchData" @checkSearch="checkSearch" @resetSearch="resetSearch"></searchDialog>
    <detailDialog :detailData="detailData" @checkRender="getData"></detailDialog>
    <titleDialog :titleData="titleData"></titleDialog>
    <transCreated :createdData="createdData" @checkRender="checkRender"></transCreated>

  </div>
</template>

<script>
import createdDialog from "./createdCustomerDialog.vue"; //新建客户
import searchDialog from "./opportSearchDialog.vue"; //高级搜索弹窗
import detailDialog from "./opportDetailDialog.vue"; //客户详情弹窗
import transCreated from '../opportunityManage/transCreated' //变更负责人

export default {
  props:['type'],
  data() {
    return {
      loading: false,
      parames: {
        tabKey: this.type,
        page: 1, //页码index
        white: false,
        limits: [10,50,100,200,500],
        limit: 10, //页码最大
        customerName: '', //联系人名称
        totalCount: 0,
        search: []
      },
      dialogData: {
        //新建弹窗
        type: '',
        title: '',
        show: false
      },
      createdData: {
        show: false
      },
      searchData: {
        //搜索弹窗
        show: false
      },
      titleData: {
        //表头列弹窗
        show: false
      },
      detailData: {
        //详情弹窗
        show: false
      },
      daochuBtn: false,
      daoruBtn: false,
      newBtn: false,
      mobanBtn: false,
      zhuanchuBtn: false,
      tableData: [],
      myHeaders: {authorization: ''},
      multipleSelection: [],
      fileAction: this.$api.postCustomerImport, //附件上传地址
    };
  },
  components: {
    createdDialog,
    searchDialog,
    detailDialog,
    transCreated
  },
  methods: {
    handleExport () {
      var search = ''
      if (this.parames.search.length>0) {
        search = encodeURIComponent(JSON.stringify(this.parames.search).toString())
      }
      window.location.href = this.$api.postCustomerExport+'?userId=' + this.$utils.getLocalStorage('user').id+'&tabKey=' + this.type + '&search=' + search
    },
    handleExport2 () {
      window.location.href = this.$api.postCustomerDownloadTemplate
    },
    checkRender () {
      this.parames.page = 1
      this.getList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleTrans () { //批量转出
      if (this.multipleSelection.length == 0) {
          this.$alert('请至少选择一个客户','提示')
          return false
      }
      let ids = []
      this.multipleSelection.forEach(el=>{
          ids.push(el.id)
      })
      this.createdData.type = 'Allkehu'
      this.createdData.ids = ids
      this.createdData.show = true
    },
    search() {
      //查询
      this.parames.page = 1;
      let searchDate = this.parames.search
      let n = 0
      this.parames.search.forEach(el=>{
        if (el.key == 'customerName') {
          n = 1
        }
      })
      if (this.parames.customerName && n == 0) {
        this.parames.search.push({
          key: 'customerName',
          value: this.parames.customerName,
          type: 'string'
        })
      } else if (this.parames.customerName && n == 1) {
        this.parames.search.forEach((el,index)=>{
          if (el.key == 'customerName') {
            el.value = this.parames.customerName
          }
        })
      } else if (!this.parames.customerName && n == 1) {
        this.parames.search.forEach((el,index)=>{
          if (el.key == 'customerName') {
            this.parames.search.splice(index,1)
          }
        })
      }
      this.getList()
    },
    handleSucess (response,fileList) { //上传附件成功返回url
      this.$message({
          message: '上传导入'+response.data.successCount+'条数据',
          type: 'success'
      });
      this.parames.page = 1
      this.getList()
    },
    resetSearch () {
      this.parames.customerName = ''
    },
    handleSearch() {
      //打开高级搜索弹窗
      this.searchData.customerName = this.parames.customerName
      this.searchData.show = true;
    },
    checkSearch (value) {
      this.parames.search = value
      value.forEach(el=>{
        if(el.key == 'customerName') {
          this.parames.customerName = el.value
        }
      })
      this.parames.page = 1
      this.getList()
    },
    checkPage () {
      this.getList()
    },
    handleSetTitle () { //设置表头列
      this.titleData.show = true
    },
    handleAdd() {//新建客户
      this.dialogData.type = 'new';
      this.dialogData.title = '新建客户';
      this.dialogData.show = true;
    },
    getData () {
      this.$post(this.$api.postCustomerCoreList,this.parames).then(response => {
        if (response.data.succeed) {
          this.parames.totalCount = response.data.totalCount
          this.tableData = response.data.data
        }
        setTimeout(() => {
          this.loading = false
        }, 200);
      })
    },
    getList() {//获取页面数据
      this.loading = true
      this.getData()
    },
    handleRow(row) {
      //点击某一行数据打开详情
      this.detailData.id = row.id;
      this.detailData.show = true;
    },
    tableRowClassName({ row, rowIndex }) {
      //斑马表格
      if (rowIndex % 2 === 1) {
        return "warning-row";
      } else {
        return "success-row";
      }
      return "";
    }
  },
  mounted() {
    this.myHeaders.authorization = this.$utils.getLocalStorage('usertoken')
    let buttonLimit = this.$utils.getLocalStorage('buttonlist')  //按钮缓存
    if (buttonLimit.indexOf('k060') !== -1) {
      this.daochuBtn = true
    }
    if (buttonLimit.indexOf('k061') !== -1) {
      this.daoruBtn = true
    }
    if (buttonLimit.indexOf('k071') !== -1) {
      this.newBtn = true
    }
    if (buttonLimit.indexOf('k075') !== -1) {
      this.mobanBtn = true
    }
    if (buttonLimit.indexOf('k080') !== -1) {
      this.zhuanchuBtn = true
    }
    this.getList()
  }
};
</script>

<style>
.el-table .warning-row {
  background: #f9f9f9;
}
.el-table .success-row {
  background: #fff;
}
</style>